﻿

class Connector extends MovieClip {
	
	// animation handlers:
	private var startAnimation:Function = null;
	private var stopAnimation:Function = null;
	private var showErrors:Boolean = true;
	
	function Connector() {
	}
	
	public function setAnimationHandlers(s:Function, t:Function):Void {
		startAnimation = s;
		stopAnimation = t;
	}
	
	public function setShowErrors(value:Boolean) {
		showErrors = value;
	}

	public function get(url:String, handler:Function):Void {
		startAnimation.call(this);
		var xml = new XML();
		xml.ignoreWhite = true;
		xml.connectorMovie = this;
		xml.onLoad = function(ok) {
  			if(ok) {
				if(this.status == 0) {
					if(this.firstChild.attributes.status != "ok") {
						if(this.connectorMovie.showErrors) {
							Toolset.alert(this.connectorMovie, "Security error. Status: "+this.firstChild.attributes.status);
						}
					}
					else {
						handler.call(this, this);
					}
				}
				else {
					if(this.connectorMovie.showErrors) {
						Toolset.alert(this.connectorMovie, "Error loading XML file. Status: "+this.status);
					}
				}
  			}
			else {
				if(this.connectorMovie.showErrors) {
					Toolset.alert(this.connectorMovie, "Error loading URL \""+url+"\"");
				}
			}
			this.connectorMovie.stopAnimation.call(this.connectorMovie);
		};
		xml.load(url);
	}

}